package com.taylor.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taylor.seckill.exection.GlobalException;
import com.taylor.seckill.mapper.UserMapper;
import com.taylor.seckill.pojo.User;
import com.taylor.seckill.service.IUserService;
import com.taylor.seckill.util.CookieUtil;
import com.taylor.seckill.util.MD5Util;
import com.taylor.seckill.util.RedisUtil;
import com.taylor.seckill.util.UUIDUtil;
import com.taylor.seckill.vo.LoginVo;
import com.taylor.seckill.vo.ResponseBean;
import com.taylor.seckill.vo.ResponseBeanEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author taylor
 * @since 2021-04-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public ResponseBean doLogin(LoginVo loginVo, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        if(StringUtils.isEmpty(mibile) || StringUtils.isEmpty(password)){
//            return ResponseBean.error(ResponseBeanEnum.LOGIN_ERROR);
//        }
//        if(!ValidatorUtil.isMobile(mibile)){
//            return ResponseBean.error(ResponseBeanEnum.LOGIN_ERROR);
//        }
        //根据手机号获取用户
        User user = userMapper.selectById(mobile);
        if(null == user){
            throw new GlobalException(ResponseBeanEnum.LOGIN_ERROR);
        }
        if(!MD5Util.formPasswordToPass(password, user.getSalt()).equals(user.getPassword()))
        {
            throw new GlobalException(ResponseBeanEnum.LOGIN_ERROR);
        }
        //UUID生成cookie
        String ticket = UUIDUtil.uuid();
//        redisUtil.set(MiaoshaUserKey.token, ticket, user);
        redisTemplate.opsForValue().set("user"+ticket,user,10, TimeUnit.MINUTES);
//        httpServletRequest.getSession().setAttribute(ticket,user);
        CookieUtil.setCookie(httpServletRequest,httpServletResponse,"userTicket",ticket,true);
        return ResponseBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        if(StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user" + userTicket);
        if(user!=null){
            CookieUtil.setCookie(httpServletRequest,httpServletResponse,"userTicket",userTicket,true);
        }
//        CookieUtil.java.setCookie(httpServletRequest,httpServletResponse,"userTicket",userTiket);
        return user;
    }
}
